<?php

namespace app\index\model;

use think\Model;

class Getitem extends Model
{
    //所有查询
    public function itemA($order='weigh',$otype='desc',$limit){
        $res = db('article')->order($order,$otype)->limit($limit)->select();
        return $res;
    }

    //普通查询
    public function item($cate,$limit=1,$order='weigh',$otype='desc'){
        $res = db('article')->where('category_id',$cate)->order($order,$otype)->limit($limit)->select();
        return $res;
    }

    //栏目中所有分类聚合查询
    public function itemOR($cate,$limit=1,$order='weigh',$otype='desc'){
        $cat="";
        for($i=0;$i<count($cate);$i++){
            if($i==count($cate)-1)
                $cat.=' category_id = '.$cate[$i];
            else
                $cat.=' category_id = '.$cate[$i].' or ';
        }
        $res=db('article')->where($cat)->order($order,$otype)->limit($limit)->select();
        return $res;
    }

    //没有限制的查询
    public function itemAll($cate,$order='weigh',$otype='desc'){
        $res = db('article')->where('category_id',$cate)->order($order,$otype)->select();
        return $res;

    }

    //单个查询是否存在
    public function check($id){
        $res = db('article')->where('id',$id)->find();
        return $res;
    }

    //增加阅读量
    public function addview($id){
        $res=db('article')->where('id',$id)->setInc('views');
        return $res;
    }

    //获取上一篇和下一篇
    public function otherArt($id,$type){
        $otype = $type=='<'?'desc':'asc';
        $res = db('article')->where('id',$type,$id)->order('id',$otype)->limit(1)->find();
        return $res;
    }

    //通过id获取分类昵称
    public function getcate($id){
        $res = db('category')->where('id',$id)->find();
        return $res;
    }

    //通过昵称获取分类信息
    public function getcateid($nick){
        $res = db('category')->where('nickname',$nick)->find();
        return $res;
    }

    //根据条目查询
    public function itemSearch($sea,$order='weigh',$otype='desc'){
        $res = db('article')->where($sea)->order($order,$otype)->find();
        return $res;
    }

    //随机
    function itemRand($limit=5){
        $num = $limit;    //需要抽取的默认条数
        $table = 'article';    //需要抽取的数据表
        $countcus = db($table)->count();    //获取总记录数
        $min = db($table)->min('id');    //统计某个字段最小数据
        if($countcus < $num){
            $num = $countcus;
        }
        $i = 1;
        $flag = 0;
        $ary = array();
        while($i<=$num){
            $rundnum = rand($min, $countcus);//抽取随机数
            if($flag != $rundnum){
                //过滤重复 
                if(!in_array($rundnum,$ary)){
                    $ary[] = $rundnum;
                    $flag = $rundnum;
                }else{
                    $i--;
                }
                $i++;
            }
        }
        $res = db($table)->where('id','in',$ary,'or')->select();
        return $res;
    }

    //模糊查询
    //获取招生简章等 模糊查询
    public function itemlike($title,$ptitle,$type,$limit){
        $res = db('article')->where($title, 'like' ,'%'.$ptitle.'%'.$type.'%')->limit($limit)->select();
        return $res;
    }
    

}